package com.mybatis.test.spring.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author Administrator
 * @create 2020-08-13 15:27
 */
@PropertySource("classpath:db.properties")
@Configuration
public class DataSourceConfig {
    @Value("${db.url}")
    private String url;
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;
    @Value("${db.driver}")
    private String driver;

    @Bean
    public DataSource dataSource() {
        Properties properties = new Properties();
        properties.put("driverClassName", driver);
        properties.put("jdbcUrl", url);
        properties.put("username", username);
        properties.put("password", password);
        /*配置初始化大小、最小、最大 */
        properties.put("dataSource.maxPoolSize", 5);
        /* 配置获取连接等待超时的时间 */
        properties.put("dataSource.validationTimeout", "60000");
        HikariConfig config = new HikariConfig(properties);
        return new HikariDataSource(config);
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}
